// Copyright (c) 2019 Presto Labs Pte. Ltd.
// Author: donggu

#include "coin2/base/rate_limiter.h"

#include <doctest.h>

DOCTEST_TEST_CASE("coin2/base/rate_limiter") {
  DOCTEST_SUBCASE("sliding rate limiter") {
    const int64_t SEC = 1'000'000'000LL;
    auto limiter = SlidingRateLimiter(10 * SEC, 3);
    DOCTEST_CHECK(limiter.Take(1 * SEC) == true);
    DOCTEST_CHECK(limiter.Take(4 * SEC) == true);
    DOCTEST_CHECK(limiter.Take(6 * SEC) == true);
    DOCTEST_CHECK(limiter.Take(7 * SEC) == false);
    DOCTEST_CHECK(limiter.Take(10 * SEC) == false);
    DOCTEST_CHECK(limiter.Take(11 * SEC) == true);  // 4, 6, 11
    DOCTEST_CHECK(limiter.Take(13 * SEC) == false);
    DOCTEST_CHECK(limiter.Take(14 * SEC) == true);  // 6, 11, 14
  }
}
